home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1996 April / Macworld (1996-04).dmg / Shareware World / Entertainment / Arcade / CheeseToast / Source / GameWindow.c < prev    next >
Text File  |  1994-03-10  |  4KB  |  172 lines

  1. /************************************************************************************
  2.  * GameWindow.c
  3.  *
  4.  * Main Window Routine
  5.  ************************************************************************************/
  6.  
  7. #include "ObjectWindow.h"
  8. #include "CToast.h"
  9.  
  10. extern Boolean    gDoneFlag;
  11.  
  12. #define        MainWIND    128
  13.  
  14. void MyNewWindow(void);
  15. void MyDraw(WindowPtr theWin);
  16. void MyHandleClick(WindowPtr theWin, Point where);
  17. void MyIdle(WindowPtr theWin, EventRecord *theEvent);
  18. void MyProcessKey(EventRecord *theEvent);
  19.  
  20.  
  21. // Create a new main window using a 'WIND' template from the resource fork
  22. //
  23. void MyNewWindow(void)
  24. {
  25.     WindowPtr    theWindow;
  26.     
  27.     // Get the Template & Create the Window, it is set up in the resource fork
  28.     // to not be initially visible 
  29.     theWindow = InitObjectWindow(MainWIND, NULL,false);
  30.     ((ObjectWindowPtr) theWindow)->Draw = MyDraw;
  31.     ((ObjectWindowPtr) theWindow)->HandleClick = MyHandleClick;
  32.     ((ObjectWindowPtr) theWindow)->Idle = MyIdle;
  33.     ((ObjectWindowPtr) theWindow)->ProcessKey = MyProcessKey;
  34.     
  35.     MoveWindow(theWindow,screenBits.bounds.left,screenBits.bounds.top,false);
  36.     SizeWindow(theWindow,screenBits.bounds.right - screenBits.bounds.left,
  37.                          screenBits.bounds.bottom - screenBits.bounds.top,false);
  38.  
  39.     // Show the window
  40.     ShowWindow(theWindow);
  41.  
  42.     // Make it the current grafport
  43.     SetPort(theWindow);
  44.  
  45.     PaintRect(&theWindow->portRect);
  46.     
  47.     InitializeGame(theWindow);
  48.     HideCursor();
  49. }
  50.  
  51. // Respond to an update event - BeginUpdate has already been called.
  52. //
  53. void MyDraw(WindowPtr theWindow)
  54. {
  55.     extern Ptr    gVideoMem,gScreenMem;
  56.     // Erase the content area
  57.     if (gVideoMem && gScreenMem)
  58.         MyCopyBits();
  59.     else
  60.         PaintRect(&theWindow->portRect);
  61. }
  62.  
  63.  
  64. // Respond to a mouse-click - highlight cells until the user releases the button
  65. //
  66. void MyHandleClick(WindowPtr theWin, Point where)
  67. {
  68.     // Mouse is not used in this game
  69. }
  70.  
  71. // When in "Attract" mode, we use normal event processing
  72. // to get keystrokes.
  73. // While playing game, we use CheckKeys function...
  74. //
  75. void MyProcessKey(EventRecord *theEvent)
  76. {
  77.     short    ch;
  78.     ch = theEvent->message & charCodeMask;
  79.     switch (gGameState) {
  80.     case GS_Attract:
  81.         switch (ch) {
  82.         case '0':
  83.         case '1':
  84.         case '2':
  85.         case '3':
  86.         case '4':
  87.         case '5':
  88.         case '6':
  89.         case '7':
  90.         case '8':
  91.         case '9':
  92.             gPrefs.soundLevel = ch - '0';
  93.             if (gPrefs.soundLevel > 7)
  94.                 gPrefs.soundLevel = 7;
  95.             SetSoundVol(gPrefs.soundLevel);
  96.             PlaySound(S_Shield, 2);
  97.             SavePreferences();
  98.             break;
  99.         case 'p':
  100.         case 'P':
  101.             BeginGame();
  102.             break;
  103.         case 'k':
  104.         case 'K':
  105.             RemapKeys();
  106.             break;
  107.         case 'q':
  108.         case 'Q':
  109.         case 27:
  110.             gDoneFlag = true;
  111.             break;
  112.         }
  113.     }
  114. }
  115.  
  116. void MyIdle(WindowPtr theWin, EventRecord *theEvent)
  117. {
  118.     switch (gGameState) {
  119.     case GS_Attract:
  120.         break;
  121.     case GS_Play:
  122.         MainGameLoop(theWin);
  123.         break;
  124.     case GS_GameOver:
  125.         EndGame();
  126.         BeginAttract();
  127.         break;
  128.     }
  129. }
  130.  
  131. // Draw the startup screen
  132. //
  133. void StartupScreen(void)
  134. {
  135.     CGrafPtr    curPort;
  136.     GDHandle    curDevice;
  137.  
  138.     GetGWorld(&curPort,&curDevice);
  139.  
  140.     SetGWorld(gOffScreen,NULL);
  141.     PaintRect(&gOffScreen->portRect);
  142.  
  143.     DisplayPicture(StartupPICT,-1,-1);
  144.  
  145.     SetGWorld(curPort,curDevice);
  146.     HideCursor();
  147.     MyCopyBits();
  148.     ShowCursor();
  149. }
  150.  
  151. // Display picture at provided coordinates
  152. // if coordinates are -1,-1, picture is centered
  153. //
  154. void DisplayPicture(short picNbr, short horiz, short vert)
  155. {
  156.     PicHandle    ph;
  157.     Rect        r;
  158.     ph = (PicHandle) GetResource('PICT', picNbr);
  159.     if (ph) {
  160.         r = (*ph)->picFrame;
  161.         if (horiz == -1)    // Center
  162.             OffsetRect(&r,(gOffScreen->portRect.right - (*ph)->picFrame.right)/2,
  163.                           (gOffScreen->portRect.bottom - (*ph)->picFrame.bottom)/2);
  164.         else
  165.             OffsetRect(&r, horiz, vert);
  166.         DrawPicture(ph,&r);
  167.         // KillPicture(ph);
  168.         ReleaseResource((Handle) ph);
  169.     }
  170. }
  171.  
  172.